package de.bazinga.rotw.treasure;

import java.util.Vector;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;
import com.jme.scene.Spatial.CullHint;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.gameModes.TreasureHunt;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.GameMessage;

public class TreasureUpdateMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	
	private boolean requestUpdate = false;
	private Vector<Integer> treasureIndizes = new Vector<Integer>();
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof TreasureUpdateMessage)) return ;

		TreasureUpdateMessage message = (TreasureUpdateMessage)msg;
		
		if(server) {
			treasureIndizes.clear();
			for(Treasure t:((TreasureHunt)game.getGameMode()).getTreasureList()) {
				if(t.isHidden()) {
					treasureIndizes.add(t.getIndex());
				}
			}
			
			((Server)game.getSyncManager()).getServer().sendToPlayer(message, getPlayerId());
		}else{
			for(int i:treasureIndizes) {
				((TreasureHunt)game.getGameMode()).getTreasureList().get(i).setIsCollidable(false);
				((TreasureHunt)game.getGameMode()).getTreasureList().get(i).setCullHint(CullHint.Always);
			}
		}
	}

	public void setTreasureIndizes(Vector<Integer> treasureIndizes) {
		this.treasureIndizes = treasureIndizes;
	}

	public Vector<Integer> getTreasureIndizes() {
		return treasureIndizes;
	}

	public void setRequestUpdate(boolean requestUpdate) {
		this.requestUpdate = requestUpdate;
	}

	public boolean isRequestUpdate() {
		return requestUpdate;
	}
	
}